package com.ruoyi.web.controller.construction;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.construction.StrategyPicture;
import com.ruoyi.system.service.construction.StrategyPictureService;
import com.ruoyi.system.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/picture")
public class StrategyPictureController extends BaseController {

    @Autowired
    private StrategyPictureService strategyPictureService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询施工攻略图片列表
     */

    @GetMapping("/list")
    public TableDataInfo list(StrategyPicture strategyPicture)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        strategyPicture.setUserId(userId);
        List<StrategyPicture> list = strategyPictureService.selectStrategyPictureList(strategyPicture);
        return getDataTable(list);
    }

    /**
     * 导出施工攻略图片列表
     */

    @GetMapping("/export")
    public AjaxResult export(StrategyPicture strategyPicture)
    {
        List<StrategyPicture> list = strategyPictureService.selectStrategyPictureList(strategyPicture);
        ExcelUtil<StrategyPicture> util = new ExcelUtil<StrategyPicture>(StrategyPicture.class);
        return util.exportExcel(list, "picture");
    }

    /**
     * 获取施工攻略图片详细信息
     */

    @GetMapping(value = "/{pictureId}")
    public AjaxResult getInfo(@PathVariable("pictureId") Integer pictureId)
    {
        return AjaxResult.success(strategyPictureService.selectStrategyPictureById(pictureId));
    }

    /**
     * 新增施工攻略图片
     */

    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody StrategyPicture strategyPicture)
    {   // System.out.println("*************************************"+file);
        System.out.println("*************************************"+strategyPicture);
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        strategyPicture.setUserId(userId);
        Long constructionStrategyId = strategyPicture.getConstructionStrategyId();
        strategyPicture.setConstructionStrategyId(constructionStrategyId);
        return toAjax(strategyPictureService.insertStrategyPicture(strategyPicture));
    }

    /**
     * 修改施工攻略图片
     */

    @PutMapping
    public AjaxResult edit(@RequestBody StrategyPicture strategyPicture)
    {
        return toAjax(strategyPictureService.updateStrategyPicture(strategyPicture));
    }

    /**
     * 删除施工攻略图片
     */

    @DeleteMapping("/{pictureIds}")
    public AjaxResult remove(@PathVariable Integer[] pictureIds)
    {
        return toAjax(strategyPictureService.deleteStrategyPictureByIds(pictureIds));
    }


    @PostMapping("/uploadImagepic")
    public Map<String,Object> up(MultipartFile file){
        String url = "/home/ruoyi/profile/xiazai/";
        return FileUtil.uploadImage(file,url);
    }
}
